package com.ruoyi.system.controller;

import java.util.*;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.arcus.ArcusUploadInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SegUploadDetail;
import com.ruoyi.system.service.ISegUploadDetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * (测试)上传表Controller
 *
 * @author ruoyi
 * @date 2022-12-26
 */
@RestController
@RequestMapping("/arcus/UploadRecord")
public class SegUploadDetailController extends BaseController
{
    @Autowired
    private ISegUploadDetailService segUploadDetailService;

    /**
     * 删除user_name和user_token下某个名称的图片
     */
    @PostMapping("/deleteImg")
    public AjaxResult deleteImgRecord(@RequestBody HashMap<String, String> uploadInfoJson){
        String userName = uploadInfoJson.get("userName");
        String uploadToken = uploadInfoJson.get("uploadToken");
        String imgName = uploadInfoJson.get("imgName");
        try{
            Long userId = segUploadDetailService.userInfoConvert(userName, true).user_id;
            SegUploadDetail d = new SegUploadDetail();
            d.setUserId(userId); d.setUploadToken(uploadToken); d.setImgName(imgName);
            List<SegUploadDetail> list = segUploadDetailService.selectSegUploadDetailList(d);
            Long id = list.get(0).getId();
            return toAjax(segUploadDetailService.deleteSegUploadDetailById(id));
        }catch (Exception e){
            return AjaxResult.error();
        }
    }

    /**
     * TODO 转换信息应该使用redis
     * 读取user_name和user_token对应的图片列表
     */
    @PostMapping("/listImg")
    public AjaxResult listImgList(@RequestBody HashMap<String, String> uploadInfoJson){
        String userName = uploadInfoJson.get("userName");
        String uploadToken = uploadInfoJson.get("uploadToken");
        try{
            Long userId = segUploadDetailService.userInfoConvert(userName, true).user_id;
            SegUploadDetail d = new SegUploadDetail();
            d.setUserId(userId); d.setUploadToken(uploadToken);
            List<SegUploadDetail> list = segUploadDetailService.selectSegUploadDetailList(d);
            List<String> imgList = new ArrayList<>();
            for(SegUploadDetail t: list){
                imgList.add(t.getImgName());
            }
            return AjaxResult.success(imgList);
        }catch (Exception e){
            return AjaxResult.error();
        }
    }

    /**
     * 读取登录用户的upload_token并逆序返回
     */
    @PostMapping("/listToken")
    public AjaxResult listToken(@RequestBody HashMap<String, String> uploadInfoJson){
        String userName = uploadInfoJson.get("userName");
        try{
            Long userId = segUploadDetailService.userInfoConvert(userName, true).user_id;
            List<String> tokenList = segUploadDetailService.selectUploadTokenByUserId(userId);
            Collections.reverse(tokenList);
            return AjaxResult.success(tokenList);
        }catch (Exception e){
            return AjaxResult.error();
        }
    }


    /**
     * 批量插入数据
     * 数据格式为： user_name, upload_token, name_list
     * user_name需要转换为用户id
     */
    @PostMapping("/insert")
    public AjaxResult insert(@RequestBody ArcusUploadInfo uploadInfoJson){
        Long userId = segUploadDetailService.userInfoConvert(uploadInfoJson.getUserName(), true).user_id;
        String res = segUploadDetailService.uploadInfoInsertMulti(userId, uploadInfoJson.getUploadToken(), uploadInfoJson.getImgList());
        if (res.equals("ok"))
            return AjaxResult.success();
        return AjaxResult.error(res);
    }


    /**
     * 查询(测试)上传表列表
     */
//    @PreAuthorize("@ss.hasPermi('arcus:UploadRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(SegUploadDetail segUploadDetail)
    {
        startPage();
        List<SegUploadDetail> list = segUploadDetailService.selectSegUploadDetailList(segUploadDetail);
        return getDataTable(list);
    }

    /**
     * 导出(测试)上传表列表
     */
    @PreAuthorize("@ss.hasPermi('arcus:UploadRecord:export')")
    @Log(title = "(测试)上传表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SegUploadDetail segUploadDetail)
    {
        List<SegUploadDetail> list = segUploadDetailService.selectSegUploadDetailList(segUploadDetail);
        ExcelUtil<SegUploadDetail> util = new ExcelUtil<SegUploadDetail>(SegUploadDetail.class);
        util.exportExcel(response, list, "(测试)上传表数据");
    }

    /**
     * 获取(测试)上传表详细信息
     */
//    @PreAuthorize("@ss.hasPermi('arcus:UploadRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(segUploadDetailService.selectSegUploadDetailById(id));
    }

    /**
     * 新增(测试)上传表
     */
//    @PreAuthorize("@ss.hasPermi('arcus:UploadRecord:add')")
    @Log(title = "(测试)上传表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SegUploadDetail segUploadDetail)
    {
        return toAjax(segUploadDetailService.insertSegUploadDetail(segUploadDetail));
    }

    /**
     * 修改(测试)上传表
     */
//    @PreAuthorize("@ss.hasPermi('arcus:UploadRecord:edit')")
    @Log(title = "(测试)上传表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SegUploadDetail segUploadDetail)
    {
        return toAjax(segUploadDetailService.updateSegUploadDetail(segUploadDetail));
    }

    /**
     * 删除(测试)上传表
     */
//    @PreAuthorize("@ss.hasPermi('arcus:UploadRecord:remove')")
    @Log(title = "(测试)上传表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(segUploadDetailService.deleteSegUploadDetailByIds(ids));
    }
}
